###########################
# Run model in parallel
# Christopher Gandrud
# MIT License
# Stan version used: 2.8.0
###########################

# Load packages
library(rio)
library(repmis)
library(DataCombine)
library(reshape2)
library(dplyr)
library(devtools)
library(rstan)
library(parallel)

# Set working directory
possibles <- c('FRTIndex/', 'git_repositories/FRTIndex/')

set_valid_wd(possibles)

## Set out width
options('width' = 200)

# Load function to subset the data frame to countries that report
# at least 1 item.
#source_url('https://raw.githubusercontent.com/FGCH/FRTIndex/master/source/miscFunctions/report_min_once.R')
source('source/miscFunctions/report_min_once.R')

# Load data
BaseSub <- import('source/RawData/wdi_fred_combined_GFDDv2015.csv')

# ---------------------------------------------------------------------------- #
#### Keep only countries that report at least 1 item for the entire period  ####
BaseSub <- report_min_once(BaseSub)

#### Data description ####
# Create country/year numbers
BaseSub$countrynum <- as.numeric(as.factor(BaseSub$iso2c))
BaseSub$yearnum <- as.numeric(as.factor(BaseSub$year))

#### Clean up ####
# Keep only complete variables
binary_vars <- names(BaseSub)[grep('^Rep_', names(BaseSub))]
BaseStanVars <- BaseSub[, c('countrynum', 'yearnum', binary_vars)]

# Data descriptions
NCountry <- max(BaseStanVars$countrynum)
NYear <- max(BaseStanVars$yearnum)
NItems <- length(binary_vars)

# Melt data so that it is easy to enter into Stan data list
MoltenBase <- melt(BaseStanVars, id.vars = c('countrynum', 'yearnum'))

# Convert item names to numeric
MoltenBase$variable <- as.factor(MoltenBase$variable) %>% as.numeric()

# Order data
MoltenReady <- arrange(MoltenBase, countrynum, yearnum, variable)

# Save Molton ready
export(MoltenReady, 'misc/stan_molten_ready.csv')
